package top.live.mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import top.live.mvc.abstracts.AbsForm;
import top.live.mvc.actions.ActionMapping;
import top.live.mvc.actions.FullControl;
import top.live.mvc.interfaces.InterForm;

public class MvcConServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public MvcConServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		AbsForm lForm = (AbsForm)FullControl.Full(request);
		Map<String, String> map = ActionMapping.getActionMap();
		String className = map.get(request.getParameter("sign"));
		InterForm inForm = null;
		try {
			Class<?> cls = Class.forName(className);
			inForm = (InterForm) cls.newInstance();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
/*		InterForm inForm = null;
		if(request.getParameter("sign").equals("top.live.mvc.javaben.LoginForm")){
			inForm = new LoginControl();
		}else if (request.getParameter("sign").equals("top.live.mvc.javaben.ZhuceForm")){
			inForm = new ZhuceControl();
		}		
		*/
		String info = inForm.execute(lForm);
		PrintWriter out = response.getWriter();
		out.println(info);
		out.flush();
		out.close();
		
//		ZhuceForm lForm = (ZhuceForm)FullControl.Full(request.getParameter("sign"), request);
//		System.out.println(lForm);
		
//	  	String username = request.getParameter("username");
//		String password = request.getParameter("password");
//		System.out.println("username:="+username+"||"+"password:="+password);
		
	/*	response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
